<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Tools Class
 *
 * @category	Admin
 */
class ToolsAction extends AdminAction
{


	public function _initialize()
	{
		parent::_initialize();
	}


	//更新缓存
	public function refreshCache()
	{
		$type = $_POST['type'];
		$step = max(1, intval($_GET['step']));

		if ($this->isPost())
		{
			if (in_array('data', $type)) {

			    /*-------------------- 重建数据缓存 -------------------*/
				D('AdminGroup')->cacheAdminGroup();
				D('Announcement')->cacheAnnouncements();
				D('Advertisement')->cacheAdvertisements();
				D('Category')->cacheCategories();
				D('CensorWord')->cacheCensorWords();
				D('Cron')->cacheCronJobs();
				D('EmailTemplate')->cacheEmailTemplates();
				D('Faq')->cacheFaqs();
				D('IpBlacklist')->cacheIpBlacklist();
				D('Link')->cacheLinks();
				D('Medal')->cacheMedals();
				D('Moderator')->cacheModerators();
				D('Navigation')->cacheNavigations();
				D('Config')->cacheConfigs();
				D('UserGroup')->cacheUserGroup();

				/*-------------------- 删除字段缓存 -------------------*/
				$fieldsDir = DATA_PATH .'_fields';
				if (false !== ($field = dir($fieldsDir)))
				{
					while (false !== ($entry = $field->read()))
					{
						if(preg_match("/\.php$/", $entry))
						{
							@unlink($fieldsDir .'/'. $entry);
						}
					}
					$field->close();
				}
			}
			/*-------------------- 删除模板缓存 ---------------------*/
			if (in_array('tpl', $type))
			{
				$dirs = array('Home', 'Admin');
				foreach ($dirs as $dir)
				{
					if (false !== ($tpl = dir(CACHE_PATH .$dir)))
					{
						while (false !== ($entry = $tpl->read()))
						{
							if(preg_match("/\.php$/", $entry))
							{
								@unlink(CACHE_PATH . $dir .'/'. $entry);
							}
						}
						$tpl->close();
					}
				}
			}
			$this->assign('jumpUrl', __ACTION__.'/step/2');
			$this->assign('loader', 1);
			$this->success(L('processing'));
		}
		else
		{
			$this->assign('step', $step);
			$this->display();
		}
	}


	//文件写入权限检查
	public function checkFilePermission()
	{
	    if ($this->isPost())
	    {
	    	$this->assign('loader', 1);
	    	$this->assign('jumpUrl', __ACTION__);
	    	$this->success(L('processing'));
	    }
		define('SITE_PUBLIC_PATH', './Public/');
		$checks = array(
			RUNTIME_PATH,
			RUNTIME_PATH .'Cache',
			RUNTIME_PATH .'Cache/Admin',
			RUNTIME_PATH .'Cache/Home',
			RUNTIME_PATH .'Data',
			RUNTIME_PATH .'Data/_fields',
			RUNTIME_PATH .'Data/_sqlBackups',
			HTML_PATH,
			RUNTIME_PATH .'Logs',
			RUNTIME_PATH .'Temp',
			SITE_PUBLIC_PATH .'Uploads',
		);
		$output = array();
		foreach ($checks as $entry)
		{
		    $output[] = array(
		        'name' => $entry,
		        'readable' => is_readable($entry),
		        'writable' => is_writable($entry),
		        'time' => filemtime($entry),
		    );
		}
		$this->assign('output', $output);
		$this->display();
	}


	//php配置信息
	public function phpinfo()
	{
		$phpinfo = phpinfo();
		exit($phpinfo);
	}

}
// End Class

/* End of file ToolsAction.class.php */
/* Location: ./App/Lib/Action/Admin/ToolsAction.class.php */
